const {
  updateWorkOrder
} = require('../../../api/workOrder')
const {
  WORD_ORDER_OPERATION_TYPE
} = require('../../../constant/index')
const {
  to,
  openLoading,
  closeLoading,
  showToast,
  backParentLoad,
  getEventData
} = getApp()
Page({

  data: {
    formData: {
      operationContent: '',
    }
  },

  onLoad() {
    this.setData({
      orderData: JSON.parse(this.options.data)
    })
  },

  onCustomTypeChange(event) {
    const {
      type
    } = getEventData(event)
    this.setData({
      [`formData.${type}`]: event.detail
    })
  },

  checkValidate() {
    const {
      operationContent
    } = this.data.formData;

    if (!operationContent) {
      showToast('请输入挂起原因');
      return false
    }
    return true
  },
  handleSubmit() {
    const validate = this.checkValidate();

    if (!validate) return;

    this.submitData()
  },

  async submitData() {
    const params = {
      operationType: WORD_ORDER_OPERATION_TYPE.WORK_ORDER_SUSPEND,
      operationContent: this.data.formData.operationContent,
      workOrderId: this.data.orderData.workOrderId,
    }

    openLoading('提交中')
    const [err] = await to(updateWorkOrder(params))
    closeLoading()
    if (!err) {
      showToast({
        message: '提交成功',
        type: 'success',
        mask: true,
        onClose: () => {
          setTimeout(() => {
            backParentLoad(1)
          }, 1000)
        }
      })
    }
  }
})